package com.bbs.controller;

import com.bbs.exception.BusinessException;
import com.bbs.pojo.Result;
import com.bbs.pojo.User;
import com.bbs.pojo.UserPrincipal;
import com.bbs.pojo.dto.UserDto;
import com.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public Result<UserDto> getUserDto(@PathVariable("id") String id) {
        return Result.withData(userService.getUserDtoById(id));
    }

    /**
     * 获取当前用户信息
     * @param userPrincipal
     * @return
     */
    @GetMapping("/users/myself")
    @PreAuthorize("isAuthenticated()")
    public Result<UserDto> getCurrentUserDto(@AuthenticationPrincipal UserPrincipal userPrincipal) {
        return Result.withData(userService.getUserDtoById(userPrincipal.getUserId()));
    }

    /**
     * 查询用户信息
     * @param userDto
     * @return
     */
    @GetMapping("/users")
    public Result<List<UserDto>> getUserDto(@RequestBody UserDto userDto) {
        return Result.succeed(userService.getUserDtos(userDto));
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PutMapping("/users")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> updateUser(@RequestBody User user) {
        return Result.build(userService.updateById(user));
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/users/register")
    public Result<Object> register(@RequestBody User user) {
        return Result.build(userService.register(user));
    }

    @GetMapping(value = "/users/avatars/{id}", produces = "image/png")
    public byte[] getUserAvatar(@PathVariable("id") String id, HttpServletResponse response) {
        User user = userService.getById(id);
        if (user == null) {
            throw new BusinessException("未找到当前用户");
        }
        response.setHeader("Content-Disposition", "attachment;fileName=" + user.getUsername() + "-avatar.png");
        return user.getAvatar();
    }

    @PostMapping("/users/avatars/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> setUserAvatar(@PathVariable("id") String id, @RequestBody byte[] avatar) {
        if (avatar == null) {
            return Result.build(false);
        }
        return Result.build(userService.setAvatarById(id, avatar));
    }

    @DeleteMapping("/users/avatars/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result<Object> removeAvatarById(@PathVariable("id") String id) {
        return Result.build(userService.RemoveAvatarById(id));
    }


    //不对外开放----------------------------------------------------
    @PutMapping("/sys/users/{id}/experience/add/{deltaExperience}")
    public Result<Object> updateExperience(@PathVariable("id") String id, @PathVariable("deltaExperience") Integer deltaExperience) {
        return Result.succeed(userService.updateExperience(id, deltaExperience));
    }

    @GetMapping("/sys/user-details/{username}")
    public Result<User> getUserDetails(@PathVariable("username") String username) {
        return Result.withData(userService.getUser(username));
    }

    @DeleteMapping("/sys/users/{id}")
    public Result<Object> removeUser(@PathVariable("id") String id) {
        return Result.succeed(userService.removeById(id));
    }

    @PutMapping("/sys/users/{id}/add-experience/{experience}")
    public Result<Object> addExperience(@PathVariable("id") String id, @PathVariable("experience") Integer experience){
        return Result.build(userService.addExperience(id, experience));
    }
}
